#include "linkqueue.h"
#include "sqstack.h"
#include<stdio.h>
#include<stdbool.h>

bool check_status(linkqueue * lq);
//球钟问题 ：使用链式队列、顺序栈
int main(){
	//1.1 创建球队列
	linkqueue * lq = queue_create();
	if(lq == NULL){
		return -1;
	}
	//1.2 赋初值
	for(int i = 1; i <= 27; i++){
		enqueue(lq, i);
		printf("%d ", i);
	}

	//2 构建三个空栈
	sqstack * s_min = stack_create(4);
	sqstack * s_five_min = stack_create(11);
	sqstack * s_hour = stack_create(11);

	//3.开始循环
	int i = 0;
	while(1){
		i++;
		if(!queue_empty(lq)){
			datatype data = dequeue(lq);
			if(!stack_full(s_min)){
				//1分钟栈未满，往里放球
				printf("s_min: %d\n", data);
				stack_push(s_min, data);
			}else {
				//1分钟栈已满，清空1分钟栈
				while(!stack_empty(s_min)){
					enqueue(lq, stack_pop(s_min));
				}
				if(!stack_full(s_five_min)){
					//5分钟栈未满，往里放球
					printf("s_five_min: %d\n", data);
					stack_push(s_five_min, data);
				}else{
					//5分钟栈已满，清空5分钟栈
					while(!stack_empty(s_five_min)){
						enqueue(lq, stack_pop(s_five_min));
					}
					if(!stack_full(s_hour)){
						//1小时栈未满，往里放球
						printf("s_hour: %d\n", data);
						stack_push(s_hour, data);
					}else{
						//1小时栈已满，清空1小时栈
						while(!stack_empty(s_hour)){
							enqueue(lq, stack_pop(s_hour));
						}
						printf("enqueue: %d\n", data);
						enqueue(lq, data);
						if(check_status(lq)){
							break;
						}
					}
				}
			}
		}
	}
	printf("total num : %d\n",i);
}

bool check_status(linkqueue * lq){
	if(lq == NULL){
		return false;
	}
	linklist p;
	p = lq->front->next;
	while(p && p->next){
		if(p->data < p->next->data){
			p = p->next;
		}else{
			return false;
		}
	}
	return true;
}

